package com.oa.pm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.joneying.common.annotation.CommonService;
import com.oa.core.PageResult;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import com.oa.core.utils.request.RFormParam;
import com.oa.pm.dto.TrainingPlanDTO;
import com.oa.pm.entity.InductionTrainingPersonnel;
import com.oa.pm.entity.TrainingPlan;
import com.oa.pm.mapper.TrainingPlanMapper;
import com.oa.pm.service.IInductionTrainingPersonnelService;
import com.oa.pm.service.ITrainingPlanService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.pm.vo.TrainingPlanListVO;
import com.oa.system.dto.TrainingPlanListDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 培训计划 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class TrainingPlanServiceImpl extends ServiceImpl<TrainingPlanMapper, TrainingPlan> implements ITrainingPlanService {

    private final TrainingPlanMapper trainingPlanMapper;

    @Autowired
    private IInductionTrainingPersonnelService inductionTrainingPersonnelService;

    @Autowired
    public TrainingPlanServiceImpl(TrainingPlanMapper trainingPlanMapper) {
        this.trainingPlanMapper = trainingPlanMapper;
    }

    @Override
    @CommonService
    public PageResult<TrainingPlan> findList(RFormParam param) {
        //查询
        Wrapper wrapper = new EntityWrapper();
        wrapper.where("is_deleted = 2");
        if(param.paramExist("beginTime")){
            wrapper.and().where("traning_time <= {0}", param.getString("beginTime"));
        }else if(param.paramExist("endTime")){
            wrapper.and().where("traning_time >= {0}", param.getString("endTime"));
        }else if(param.paramExist("")){
            wrapper.and().where("traning_time >= {0}", param.getString("endTime"));
        }
        List<TrainingPlan> list = trainingPlanMapper.selectPage(param.getPage(), wrapper);
        return new PageResult<>(list);
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public TrainingPlan findTrainingPlanById(String id){
        return trainingPlanMapper.findById(id);
    }

    @Transactional
    @Override
    public boolean addTrainingPlan(TrainingPlanDTO trainingPlanDTO) {
        if(insert(trainingPlanDTO.getTrainingPlan())){
            List<InductionTrainingPersonnel> inductionTrainingPersonnels = trainingPlanDTO.getInductionTrainingPersonnels();
            if(trainingPlanDTO.getInductionTrainingPersonnels() != null && trainingPlanDTO.getInductionTrainingPersonnels().size() > 0){
                inductionTrainingPersonnels.forEach(d -> {
                    d.setTrainingId(trainingPlanDTO.getTrainingPlan().getId());
                    d.setType(2);
                });
                if(inductionTrainingPersonnelService.insertBatch(inductionTrainingPersonnels)){
                    return true;
                }
            }
            return true;
        }
        return false;
    }

    @Transactional
    @Override
    public boolean editTrainingPlan(TrainingPlanDTO trainingPlanDTO) {
        if(updateById(trainingPlanDTO.getTrainingPlan())){
            if(trainingPlanDTO.getInductionTrainingPersonnels() != null && trainingPlanDTO.getInductionTrainingPersonnels().size() > 0){
                Wrapper wrapper = new EntityWrapper();
                wrapper.where("training_id = {0} and type = 2", trainingPlanDTO.getTrainingPlan().getId());
                if(inductionTrainingPersonnelService.delete(wrapper)){
                    List<InductionTrainingPersonnel> inductionTrainingPersonnels = trainingPlanDTO.getInductionTrainingPersonnels();
                    inductionTrainingPersonnels.forEach(d -> {
                        d.setTrainingId(trainingPlanDTO.getTrainingPlan().getId());
                        d.setType(2);
                    });
                    if(inductionTrainingPersonnelService.insertBatch(inductionTrainingPersonnels)){
                        return true;
                    }
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public PageResult<TrainingPlanListVO> findTrainingPlanList(TrainingPlanListDTO trainingPlanListDTO) {
        Page page = trainingPlanListDTO.getPage();
        return new PageResult<>(trainingPlanMapper.findTrainingPlanList(page, trainingPlanListDTO), page.getPages());
    }

    @Override
    public void export(TrainingPlanListDTO trainingPlanListDTO, HttpServletResponse response) {
        List<TrainingPlanListVO> trainingPlans;
        if(trainingPlanListDTO.getPage() != null){
            Page page = trainingPlanListDTO.getPage();
            trainingPlans = trainingPlanMapper.findTrainingPlanList(page, trainingPlanListDTO);
        }else{
            trainingPlans = trainingPlanMapper.findTrainingPlanList(new Page(), trainingPlanListDTO);
        }
        trainingPlans.forEach(d -> d.setInductionTrainingPersonnels(null));
        String[] rows = {"id", "培训时间", "培训主题", "培训目的", "培训内容", "培训讲师", "缺席人员及原因", "培训对象", "培训形式", "培训课时", "培训地点", "费用预算", "备注"};
        try {
            ExcelUtil.downloadExcel("技能提升培训记录" + DateUtil.getDayMonth(), rows, trainingPlans, "技能提升培训记录" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
